package com.happy.system.service.impl;

import com.happy.common.core.exception.ServiceException;
import com.happy.common.core.utils.StringUtils;
import com.happy.common.core.utils.bean.BeanValidators;
import com.happy.common.core.utils.file.FileUtils;
import com.happy.system.domain.Employee;
import com.happy.system.mapper.EmployeeMapper;
import com.happy.system.service.IEmployeeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.io.*;
import java.util.List;

import static com.happy.common.core.constant.FileConstants.baseDir;
import static com.happy.common.core.constant.FileConstants.faceImage;

@Service
public class EmployeeServiceImpl implements IEmployeeService {
    private static final Logger log = LoggerFactory.getLogger(EmployeeServiceImpl.class);

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    protected Validator validator;

    @Override
    public List<Employee> selectEmployeeList(Employee employee) {
        return employeeMapper.selectEmployeeList(employee);
    }

    @Override
    public int insertEmployee(Employee employee) {
        int row= employeeMapper.insertEmployee(employee);
        return row;
    }

    @Override
    public boolean checkPhoneUnique(Employee employee) {
        if(employeeMapper.checkPhoneUnique(employee.getPhone())==null)
            return true;
        return false;
    }

    @Override
    public boolean checkEmailUnique(Employee employee) {
        if(employeeMapper.checkEmailUnique(employee.getEmail())==null)
            return true;
        return false;
    }

    @Override
    public String importEmployee(List<Employee> employeeList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(employeeList) || employeeList.size() == 0) {
            throw new ServiceException("导入员工数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Employee employee: employeeList) {
            try {
                // 验证是否存在这个用户
                Employee u = employeeMapper.checkPhoneUnique(employee.getPhone());
                Employee u1=employeeMapper.checkEmailUnique(employee.getEmail());
                if (StringUtils.isNull(u)&&StringUtils.isNull(u1)) {
                    BeanValidators.validateWithException(validator, employee);
                    employeeMapper.insertEmployee(employee);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + employee.getName() + " 导入成功");
                }
                else if (isUpdateSupport) {
                    BeanValidators.validateWithException(validator, employee);
                    //checkUserAllowed(u);
                    //checkUserDataScope(u.getUserId());
                    employee.setEmployeeId(u.getEmployeeId());
                    employeeMapper.updateEmployee(employee);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + employee.getName() + " 更新成功");
                }
                else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、账号 " + employee.getName()+ " 已存在");
                }
            }
            catch (Exception e){
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + employee.getName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public int deleteEmployeeByIds(Long[] employeeIds) {
        //TODO 删除报告
        return employeeMapper.deleteEmployee(employeeIds);
    }

    @Override
    public Employee queryEmployeeById(Long employeeId){
        Employee employee = employeeMapper.queryEmployeeById(employeeId);
        return employee;
    }

    @Override
    public void EditEmployee(Employee employee) {
        employeeMapper.updateEmployee(employee);
    }

}
